package com.myCommon.security.exceptionHandler;

import com.alibaba.fastjson.JSON;
import com.myCommon.util.resultUtil.R;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
      @Override
      public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
            R fail = R.fail(500, "访问权限不足，请先添加权限");
            String result = JSON.toJSONString(fail);
            response.setStatus(500);
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/json");
            response.getWriter().print(result);
            
      }
}
